var ns;
app = new Vue({
    el: '#app',
    data: {
        list:[],
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        showtime:false,
        ProjectName:'',
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        summary: {
            completePercent:'',
        },
        projectList: [], 
        activeTransform: '',
        userModel:{},
        loginuserid:'',
    },
    methods: {
        get_data: function () {
            var that = this;
            that.get_loginuser();
            that.getSearchParams();
            setTimeout(() => {
                that.doSearch();
            }, 300);
            
        },
        get_loginuser: function() {
            var that = this;
            var options={};
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.loginuserid=data.id;
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            },{
                command: 'App/Auth/GetUser'
            });
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            var index = that.form.ProjectIDs.indexOf(ID);
            if (index > -1) {
                that.form.ProjectIDs.splice(index, 1);
            } else {
                that.form.ProjectIDs.push(ID);
            }
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        for (let i = 0; i < that.projectList.length; i++) {
                            if(that.loginuserid==that.projectList[i].value){
                                that.doSelectProject(that.projectList[i].value,that.projectList[i].label);
                            }      
                        }
                        // that.doSelectProject(that.projectList[0].value,that.projectList[0].label); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            },{
                command: 'App/Service/GetUsers'
            });
        },
        onShowTime:function(IsShow){
            var that=this;
            that.showtime=IsShow;
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            // if (type == 2 && that.showsearchmonth) {
            //     setTimeout(function () {
            //         that.initPicker();
            //     }, 100);
            // }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        //查看人员详情
        do_persondetail:function(id){
            var that=this;
            ns.openWin('person_detail_frm', '详情',{
                id:id,
                startTime:moment(that.form.startMonth).startOf('month').format('YYYY-MM-DD'),
                endTime:moment(that.form.startMonth).endOf('month').format('YYYY-MM-DD')
            });
        },
        getAnalysisData: function () {
            var that = this;
            //初始化
            that.XJlist=[];
            that.WBlist=[];
            that.userList=[];
            var planType = 1; ////planType 1 设备巡检  3 安防巡更  4 环境保洁
            if (that.ReportType == 1) {
                planType = 3;
            }
            if (that.ReportType == 2) {
                planType = 4;
            }
            var startTime=moment(that.form.startMonth).startOf('month').format('YYYY-MM-DD');
            var endTime=moment(that.form.startMonth).endOf('month').format('YYYY-MM-DD');
            var options = {};
            options.Ids = JSON.stringify(that.form.ProjectIDs);
            options.startTime = startTime;
            options.endTime = endTime;
            ns.post(options, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    var newlist = data;
                        newlist = newlist.map(res => {
                            res.completePercent = res.completeCount/(res.totalCount)||0;
                            return res
                        })
                        that.list=newlist;
                    for (let i = 0; i < that.list.length; i++) {
                        that.formatactiveTransform(that.list[i].completePercent);
                    }
                } 
                else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                command: 'App/Service/KPISummary'
            });
        },
        formatactiveTransform: function(completePercent) {
            var percent = completePercent*100;
            if (percent <= 50) {
                this.activeTransform = 'transform:rotate(' + (percent / 100) * 360 + 'deg)';
            } else {
                this.activeTransform = 'transform:rotate(' + ((percent / 100) * 360 - 180) + 'deg)';
            }
        },

        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //月份
                    var btn = document.querySelector('#datemonth');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startMonth = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startMonth = rs.value;
                                    _this.doSearch();
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    var nowDate = new Date();
    var Y = nowDate.getFullYear();
    var M = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    app.form.startMonth = Y+'-'+M;//初始化默认当前月
    app.ReportType=api.pageParam.servicetype || 0;
    app.initPicker();
    app.get_data();

};
